package org.generic.test03;

import java.util.*;

/**
 * @author CSY
 * @date 2025 - 03 - 16
 */
public class GenericExercise {
    public static void main(String[] args) {
        //使用泛型的方式给HashSet放入三个学生对象
        HashSet<Student> stus = new HashSet<>();
        stus.add(new Student("jack",18));
        stus.add(new Student("mack",18));
        stus.add(new Student("tom",18));

        //遍历 - 由于已经使用呢泛型指定类型，所以在遍历的时候可以直接指定Student类型而不报错
        for (Student stu : stus) {
            System.out.println(stu);
        }
        //使用泛型的方式给HashMap放入三个学生对象
        HashMap<String, Student> hm = new HashMap<>();
        hm.put("zs", new Student("zs",18));
        hm.put("ls", new Student("ls",18));
        hm.put("ww", new Student("ww",18));

        //迭代器 EntrySet
        Set<Map.Entry<String,Student>> entries = hm.entrySet();
        Iterator<Map.Entry<String, Student>> iterator = entries.iterator();

        while (iterator.hasNext()) {
            Map.Entry<String, Student> next =  iterator.next();
            System.out.println(next.getKey() + "-" + next.getValue());
        }
    }
}
/**
 * 创建三个学生对象
 * 放入HashSet中 类型使用学生对象
 * 放入HashMap中，要求Key是String name,Value时学生对象
 * 使用两种方式遍历
 */
class Student{
    private String name;
    private int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}